package com.bhp.security.auth;

import com.bhp.security.entity.Role;
import com.bhp.security.entity.UserAuth;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *
 */
@Data
public class Auth extends UserAuth implements UserDetails {

    private List<Role> roles;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        // 已获得的权限
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (Role role : roles) {
            authorities.add(new SimpleGrantedAuthority(role.getCode()));
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return getPwd();
    }

    @Override
    public String getUsername() {
        return getName();
    }

    // 账号未过期?
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    // 账号未被锁定?
    @Override
    public boolean isAccountNonLocked() {
        return getLocked();
    }

    // 密码未过期?
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    // 账号是否被删除?
    @Override
    public boolean isEnabled() {
        return getEnabled();
    }
}
